package com.bluepunch.result;

import lombok.Data;

//全局统一返回结果类
@Data
public class Result<T> {
    //返回状态码
    private Integer code;
    //返回消息
    private String message;
    //返回数据
    private T data;


    public Result() {

    }

    //封装返回数据(data)
    protected static<T> Result<T> build(T data){
        Result<T> result = new Result<>();
        if (data != null){
            result.setData(data);
        }
        return result;
    }

    //封装返回数据(data,code,message)
    protected static<T> Result<T> build(T data,Integer code, String message){
        Result<T> result = build(data);
        //设置状态码
        result.setCode(code);
        //设置返回信息
        result.setMessage(message);
        return result;
    }

    //封装返回数据(data,resultCodeEnum)
    public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) {
        Result<T> result = build(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    //操作成功 -- 无数据
    public static<T> Result<T> ok(){
        return Result.ok(null);
    }
    //操作成功 -- 有数据
    public static<T> Result<T> ok(T data){
        Result<T> result = build(data);
        return build(data, ResultCodeEnum.SUCCESS);
    }

    //操作失败 -- 无数据
    public static<T> Result<T> fail(){
        return Result.fail(null);
    }
    //操作失败 -- 有数据
    public static<T> Result<T> fail(T data){
        Result<T> result = build(data);
        return build(data, ResultCodeEnum.FAIL);
    }

    //定义 扩展自定义信息
    public Result<T> message(String msg){
        this.setMessage(msg);
        return this;
    }

    //定义 扩展自定义响应码
    public Result<T> code(Integer code){
        this.setCode(code);
        return this;
    }
}
